package com.lianniu.mgr.demand.service.impl;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.demand.dao.IDemandDao;
import com.lianniu.mgr.demand.service.IDemandService;

import com.lianniu.mgr.demand.vo.*;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
import org.apache.ibatis.annotations.Param;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-05-14.
 */
@Service
public class DemandServiceImpl implements IDemandService {

    private static final Logger logger = LoggerFactory.getLogger(DemandServiceImpl.class);
    @Autowired
    IDemandDao demandDao;


    @Override
    public BaseResult getDemandListForPage(DemandConditionVo condition, PaginationInfo paginationInfo) {
        condition.setPaginationInfo(paginationInfo);
        List<DemandVo> demandList = demandDao.getDemandListForPage(condition);
        Map<String, Object> retmap = new HashMap<String, Object>();
        retmap.put("total", paginationInfo.getTotalRecord());
        retmap.put("curPage", paginationInfo.getCurPage());
        retmap.put("rows", demandList);

        return BaseResult.success(retmap);
    }

    @Override
    public BaseResult updateDemandDelFlag(String demandID) {
        // TODO Auto-generated method stub
        logger.info("updateDemandDelFlag begin");
        logger.info("updateDemandDelFlag demandID:" +demandID.toString());
        logger.info("updateDemandDelFlag: input data check begin");

        BaseResult baseResult;
        if (StringUtil.isEmpty(demandID.toString())) {
            baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
            return baseResult;
        }
        logger.info("updateDemandDelFlag: input data check end");

        return BaseResult.success(demandDao.updateDemandDelFlag(demandID));
    }

    @Override
    public BaseResult getDemandById(String demandId) {
        logger.info("根据ID获取需求单" + demandId);
        DemandVo demand = demandDao.getDemandById(demandId);
        logger.info(String.format("获取到的需求单信息[%s]", JSON.toJSONString(demand)));
        return BaseResult.success(demand);
    }

    @Override
    public BaseResult modifyDemand(DemandVo demandVo, DemandDetailVo demandDetailVo) {
        logger.info("modifyDemand begin");
        logger.info("modifyDemand demandId:" +demandVo.getId().toString());
        logger.info("modifyDemand: input data check begin");

        BaseResult baseResult;
        if (StringUtil.isEmpty(demandVo.getId().toString())) {
            baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
            return baseResult;
        }
        int demand=demandDao.updateDemand(demandVo);
        int demanddetail=demandDao.updateDemandDetail(demandDetailVo);

        logger.info("modifyDemand: input data check end");

        return BaseResult.success(demand+demanddetail);
    }

    @Override
    public BaseResult getDemandSupplyListForPage(DemandSupplyConditionVo condition, PaginationInfo paginationInfo) {
        condition.setPaginationInfo(paginationInfo);
        List<DemandSupplyVo> demandSupplyList = demandDao.getDemandSupplyListForPage(condition);
        Map<String, Object> retmap = new HashMap<String, Object>();
        retmap.put("total", paginationInfo.getTotalRecord());
        retmap.put("curPage", paginationInfo.getCurPage());
        retmap.put("rows", demandSupplyList);

        return BaseResult.success(retmap);
    }

    @Override
    public BaseResult updateDemandSupplyDelFlag(String id) {
        // TODO Auto-generated method stub
        logger.info("updateDemandSupplyDelFlag begin");
        logger.info("updateDemandSupplyDelFlag id:" +id.toString());
        logger.info("updateDemandSupplyDelFlag: input data check begin");

        BaseResult baseResult;
        if (StringUtil.isEmpty(id.toString())) {
            baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
            return baseResult;
        }
        logger.info("updateDemandSupplyDelFlag: input data check end");

        return BaseResult.success(demandDao.updateDemandSupplyDelFlag(id));
    }

    @Override
    public BaseResult getDemandSupplyById(String id) {
        logger.info("根据ID获取报价单" + id);
        DemandSupplyVo demandSupply = demandDao.getDemandSupplyById(id);
        logger.info(String.format("获取到的报价单信息[%s]", JSON.toJSONString(demandSupply)));
        return BaseResult.success(demandSupply);
    }

    @Override
    public BaseResult modifyDemandSupply(DemandSupplyVo demandSupplyVo) {
        logger.info("modifyDemandSupply begin");
        logger.info("modifyDemandSupply id:" +demandSupplyVo.getId().toString());
        logger.info("modifyDemandSupply: input data check begin");

        BaseResult baseResult;
        if (StringUtil.isEmpty(demandSupplyVo.getId().toString())) {
            baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
            return baseResult;
        }
        int demandSupply=demandDao.updateDemandSupply(demandSupplyVo);

        logger.info("modifyDemandSupply: input data check end");

        return BaseResult.success(demandSupply);
    }
}
